package com.dormitory.management.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dormitory.management.system.entity.Cost;
import com.dormitory.management.system.entity.CostExcel;
import com.dormitory.management.system.req.CostPageReq;
import com.dormitory.management.system.req.CostSaveReq;
import com.dormitory.management.system.resp.CostResp;
import com.dormitory.management.system.service.CostService;
import com.dormitory.management.system.utils.ExcelUtil;
import com.dormitory.managemnet.util.core.Result;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 水电费用表 前端控制器
 * </p>
 *
 * @author
 * @since 2021-12-17
 */
@Api(tags = "水电管理")
@RestController
@RequestMapping("/cost")
public class CostController {

    @Autowired
    private CostService costService;

    @ApiOperation("添加水电记录")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Result<String> save(@RequestBody CostSaveReq req){
        if(!costService.save(req)) {
            return Result.error("添加失败！");
        }
        return Result.success("添加成功！");
    }

    @ApiOperation("删除水电记录")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result<String> delete(
            @ApiParam(value = "水电id",required = true)
            @PathVariable Long id){
        if(!costService.delete(id)) {
            return Result.error("删除失败！");
        }
        return Result.success("删除成功！");
    }

    @ApiOperation("水电记录分页列表")
    @RequestMapping(value = "/getPage",method = RequestMethod.POST)
    public Result<IPage<CostResp>> getPage(@RequestBody CostPageReq req){
        return Result.ok(costService.getPage(req));
    }

    @ApiOperation("缴费")
    @RequestMapping(value = "/pay/{id}",method = RequestMethod.GET)
    public void pay(
            @ApiParam(value = "水电id",required = true)
            @PathVariable Long id, HttpServletResponse response) throws IOException {
        //配置响应信息
        response.setContentType("image/jpg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        OutputStream stream = response.getOutputStream();
        //获取二维码图片
        BitMatrix bitMatrix = costService.pay(id);
        //以流的形式输出到前端显示
        MatrixToImageWriter.writeToStream(bitMatrix,"jpg" , stream);
    }

    @ApiOperation("更新缴费状态")
    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    public Result<String> updateStatus(HttpServletRequest request) {
        return Result.success(costService.updateStatus(request));
    }

    @ApiOperation("Excel导入水电费表")
    @PostMapping("/createByExcel")
    @ResponseBody
    public Result createWeekCheck(String username, MultipartFile file)throws IOException {
        return costService.importWeekCheckByExcel(username,file);
    }

    @ApiOperation("Excel模板下载")
    @GetMapping("/excel/template")
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导入水电费模板";
        String sheetName = "导入水电费模板";
        List<CostExcel> CostExcelList = new ArrayList<>();
        CostExcelList.add(new CostExcel());
        try {
            ExcelUtil.writeExcel(response, CostExcelList, fileName, sheetName, Cost.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
